<!DOCTYPE html>
<?php
include('_includes/lock.php');
include ('_class/Navigator.php');
include ('_class/DataBaseConnector.php');

$error = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $target_path = $_SERVER["DOCUMENT_ROOT"] . "/cms-cs/pdf/" . $_FILES['uploadedfile']['name'];

    if (is_uploaded_file($_FILES['uploadedfile']['tmp_name'])) {
        if ($_FILES['uploadedfile']['type'] != "application/pdf") {
            $error = "<p>Documents must be uploaded in PDF format.</p>";
        } else {
            if (move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
                $db = new DataBaseConnector();
                $db->connect();
                $parent = addslashes($_POST['parent']);
                $title = $_FILES['uploadedfile']['name'];
                $bodytext = $target_path;
                $isRoot = 0;
                $user = $_SESSION['logged_user'];
                $db->insertData("articles", "NULL,'$title','$bodytext',CURDATE(),true,'$isRoot','$user', $parent");
                $lastID = mysql_insert_id();
              //  header("location: entry.php?id=$lastID");
            } else {
                $error = "There was an error uploading the file, please try again!";
            }
        }
    }
}
?>

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <link rel="stylesheet" href="resources/styles.css" type="text/css" media="screen" />
        <title>New Entry</title>
    </head>
    <body>
        <div id="wrapper">
            <header></header>
            <?php include ('_includes/nav.php') ?>
            <div id="page">
                <div id="content">
                    <?php echo $error ?>
                    <form enctype="multipart/form-data" method="POST">
                        <?php
                        if (isset($_GET['id'])) {
                            $id = $_GET['id'];
                            echo "<input name=\"parent\" id=\"parent\" type=\"text\" value=\"$id\" hidden=\"true\"/><br/>";
                        } else {
                            echo "<input name=\"parent\" id=\"parent\" type=\"text\" value=\"0\" hidden=\"true\"/><br/>";
                        }
                        ?>
                        <label>Choose a file to upload:</label>
                        <input name="uploadedfile" type="file" /><br />
                        <input type="submit" value="Upload File" />
                    </form>
                </div>
                <?php include ('_includes/sidebar.php') ?>
            </div>
            <footer>
                <section>
                    <p>Copyright ©2012 - All Rights Reserved</p>
                </section>
            </footer>
        </div>
    </body>
</html>
